package com.ciberiasoluciones.cibervoto.servlet;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author IngJarava
 */
@WebServlet(name = "Servlet_Cargar_Candidatos", urlPatterns = {"/Candidatos"})
public class Servlet_Cargar_Candidatos extends HttpServlet {

    private HttpSession             session_actual;
    private String text_eror        = null;
    private String text_info_error  = null;
    
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        session_actual = request.getSession(true);
        
       synchronized(session_actual)
       {
        
       
        int p_candidato = 0;
        int p_cargo     = 0;
        String salida;
        
        String select[]         = request.getParameterValues("candidato"); 
        String codmiembro       = null;
        String codproelectoral  = null;
        
        
        try
        {
            /* TODO output your page here. You may use following sample code. */
           
            if (select != null && select.length != 0)
            {
              for (int i = 0; i < select.length; i++)
              {
                salida = select[i];
                p_candidato     =   Integer.parseInt(salida.split("-")[0]);
                p_cargo         =   Integer.parseInt(salida.split("-")[1]);
                codmiembro      =   salida.split("-")[2];
                codproelectoral =   salida.split("-")[3];

                if(p_candidato!=0 && p_cargo !=0 && codproelectoral!=null && codmiembro!=null )
                {
                    session_actual.setAttribute("codproelectoral",  codproelectoral);
                    session_actual.setAttribute("codmiembro",       codmiembro);
                    session_actual.setAttribute("p_candidato",      String.valueOf( p_candidato));
                    session_actual.setAttribute("p_cargo",          String.valueOf(p_cargo));
                    
                    response.sendRedirect("confirmaciontarjeton.jsp");
                    
                    
                }
                else{
                    out.print("Error en p_candidato: "+p_candidato+"<br/>");
                    out.print("Error en p_cargo: "+p_cargo+"<br/>");
                    out.print("Error en codproelectoral: "+codproelectoral+"<br/>");
                    out.print("Error en codmiembro: "+codmiembro+"<br/>");
                    
                }
              }
            }else
            {
                text_info_error = "Selección de Candidato";
                text_eror = "Usted No ha selecionado el candidato.\n" +
"                                <br/><strong>Vuelva a intentarlo.</strong>";
                request.setAttribute("codmiembro",       codmiembro);    
                request.setAttribute("text_eror", text_eror);
                request.setAttribute("text_info_error", text_info_error);
                request.getRequestDispatcher("noselecion.jsp").forward(request, response);
                
                out.print("Error en select: "+select);
            }           
        }  /*catch(java.lang.Exception e){
            out.println("<html>");
            out.println("<head>");
            out.println("<title>ERROR en Cargar Candidatos</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>ERROR EN EL SERVER</h1>");
            out.println("<p>"+e+"</p>");
            out.println("</body>");
            out.println("</html>");
        } */
        finally {            
            out.close();
        }
       }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
    
    
}
